package cn.iocoder.yudao.module.resource.service.tagclassify;

import java.util.stream.Collectors;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.resource.controller.admin.tagclassify.vo.*;
import cn.iocoder.yudao.module.resource.dal.dataobject.tagclassify.TagClassifyDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.resource.dal.mysql.tagclassify.TagClassifyMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.resource.enums.ErrorCodeConstants.*;

/**
 * 标签分类 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class TagClassifyServiceImpl implements TagClassifyService {

    @Resource
    private TagClassifyMapper tagClassifyMapper;

    @Override
    public Integer createTagClassify(TagClassifySaveReqVO createReqVO) {
        TagClassifyDO tagClassifyDO = tagClassifyMapper.getOne(createReqVO.getName());
        if(tagClassifyDO != null){
            throw exception(TAG_CLASSIFY_HAD_EXISTS);
        }
        if(createReqVO.getParentId() != 0) {
            this.validateTagClassifyExists(createReqVO.getParentId());
        }
        // 插入
        TagClassifyDO tagClassify = BeanUtils.toBean(createReqVO, TagClassifyDO.class);
        tagClassifyMapper.insert(tagClassify);
        // 返回
        return tagClassify.getId();
    }

    @Override
    public void updateTagClassify(TagClassifySaveReqVO updateReqVO) {
        // 校验存在
        validateTagClassifyExists(updateReqVO.getId());
        // 更新
        TagClassifyDO updateObj = BeanUtils.toBean(updateReqVO, TagClassifyDO.class);
        tagClassifyMapper.updateById(updateObj);
    }

    @Override
    public void deleteTagClassify(Integer id) {
        // 校验存在
        validateTagClassifyExists(id);
        // 删除
        tagClassifyMapper.deleteById(id);
    }

    private void validateTagClassifyExists(Integer id) {
        if (tagClassifyMapper.selectById(id) == null) {
            throw exception(TAG_CLASSIFY_NOT_EXISTS);
        }
    }

    @Override
    public TagClassifyDO getTagClassify(Integer id) {
        return tagClassifyMapper.selectById(id);
    }

    public void setParentClassify(List<TagClassifyRespVO> tagClassifyDOList){
        List<Integer> idsList = tagClassifyDOList.stream().map(c->c.getParentId()).collect(Collectors.toList());
        List<TagClassifyDO> tagClassifyDOS = tagClassifyMapper.selectBatchIds(idsList);
        Map<Integer,TagClassifyDO> tagClassifyDOMap = tagClassifyDOS.stream().collect(Collectors.toMap(TagClassifyDO::getId,c->c));
        for(TagClassifyRespVO tagClassifyVo : tagClassifyDOList){
            TagClassifyDO parent = tagClassifyDOMap.get(tagClassifyVo.getParentId());
            if(parent != null){
                tagClassifyVo.setParentName(parent.getName());
            }
        }
    }


    @Override
    public PageResult<TagClassifyDO> getTagClassifyPage(TagClassifyPageReqVO pageReqVO) {
        return tagClassifyMapper.selectPage(pageReqVO);
    }

}